import { Account, redis } from '~/db'

/**
 * 获取账号
 * @param appid
 * @param component_app_id
 * @param type
 */
export async function getAccount(app_id: string, component_app_id: string = ''): Promise<Account | null | undefined> {
    //先尝试通过redis获取
    const cache = await redis.get(`account:${app_id}:${component_app_id}`)
    if (cache) {
        return new Account(JSON.parse(cache), { isNewRecord: false })
    }

    const row = await Account.findOne({
        where: {
            app_id,
            component_app_id,
        },
    })
    if (row) {
        await redis.set(`account:${app_id}:${component_app_id}`, JSON.stringify(row.toJSON()))
        return row
    }
}
